package com.shenzhenai.composesideeffects.samples

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * @author huangzehui
 * @date 20/9/2025 下午10:09 周六
 * @description
 *
 * 85-derivedStateOf
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.videopod.episodes&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=85
 */

// 扩展函数，检查字符串是否包含指定集合中的任一字符串
fun String.containsWord(list: List<String>): Boolean {
    for (item in list) {
        if (this.lowercase().contains(item.lowercase())) return true
    }

    return false
}

@Composable
fun ItemText(text: String, modifier: Modifier = Modifier) {
    Text(
        text = text,
        textAlign = TextAlign.Center,
        modifier = modifier
            .height(30.dp)
            .fillMaxSize()
    )
//    Text(
//        text = "垂直居中文本",
//        modifier = Modifier
//            .fillMaxWidth() // 水平撑满，使textAlign生效
//            .height(50.dp) // 设置一个固定高度
//            .wrapContentHeight(Alignment.CenterVertically) // 关键：在固定高度内垂直居中
//            .background(Color.Yellow), // 背景色仅用于演示高度区域
//        textAlign = TextAlign.Center, // 实现水平居中
//        fontSize = 18.sp
//    )
}


@Composable
fun TodoList(
    highPriorityWords: List<String> = listOf("Review", "Unlock", "Compose"),
) {

    val todoTasks = remember {
        mutableStateListOf<String>()
    }

    val highPriorityTasks = remember(todoTasks, highPriorityWords) {
        /**
         * derivedStateOf 就是将 todoTasks 这个状态和 highPriorityWords 衍生出另外的一个集合（状态）
         */
        derivedStateOf {
            todoTasks.filter { it.containsWord(highPriorityWords) }
        }
    }


    val (text, setText) = remember {
        mutableStateOf("")
    }

    Column {
        Row(verticalAlignment = Alignment.CenterVertically) {
            TextField(value = text, onValueChange = setText)
            Button(onClick = {
                todoTasks.add(text)
            }) {
                Text(text = "Add")
            }
        }

        LazyColumn {
            // 高优先级任务
            items(items = highPriorityTasks.value) {
                ItemText(text = it, modifier = Modifier.background(Color.LightGray))
            }


            // 所有任务
            items(items = todoTasks) {
                ItemText(text = it)
            }
        }

    }
}


@Preview
@Composable
fun DerivedStateOfSamplePreview() {
    TodoList()
}